import { getUserInfo, loginApi, userInfoById } from '@/api/user'
import { setToken, getToken } from '@/utils/auth'

export default {
  namespaced: true,
  state: {
    token: getToken(),
    userInfo: {}
  },
  mutations: {
    setToken(state, token) {
      state.token = token
    },
    setUserInfo(state, data) {
      state.userInfo = data
    }
  },
  actions: {
    async login(context, data) {
      const res = await loginApi(data)
      context.commit('setToken', res)
      setToken(res)
    },
    async getUserInfo(context) {
      const res = await getUserInfo()
      const res1 = await userInfoById(res.userId)
      context.commit('setUserInfo', { ...res, ...res1 })
    }
  }
}
